<?php
  include_once '../Model/GCM.php';

  class forPhoneController{
    public function qryWaitingTime($mysql, $link, $shopid, $orderofnum){
       //此處要用演算法算出時間
         //先取得店內與orderofnum相近的桌數
       if($orderofnum>0 AND $orderofnum <= 8){
         //$tmp 代表著桌種，$tmp1 代表相近的桌種
         if($orderofnum<=2){
          $tmp = 2;
           $tmp1 = 1;
         }elseif($orderofnum<=4){
           $tmp = 4;
           $tmp1 = 3;
         }elseif($orderofnum<=6){
           $tmp = 6;
           $tmp1 = 5;
         }else{
           $tmp = 8;
           $tmp1 = 7;
         }
         $tabletype = $tmp;

         //$row_getTablenum[0] 代表桌種，$row_getTablenum[1]代表桌種數量
         $sql_getTablenum = "SELECT type, totalnumber FROM tables WHERE shopid = '$shopid' AND type = $tmp;";
         $row_getTablenum = $mysql->query_row($sql_getTablenum, $link);

         //算出排隊人數(flag = -1)
         //$row_getQueue算出目前占據一樣桌種的數量
         $sql_getQueue = "SELECT COUNT(*) FROM (SELECT * FROM Client WHERE shopid = '$shopid' AND flag = '-1' AND (orderofnum = '$tmp'  OR orderofnum = '$tmp1')) AS tmp";
         $row_getQueue = $mysql->query_row($sql_getQueue, $link);

       //從statistics取出與$CheckTime同樣的weekday
         //判斷出是早、中、下、晚，用 $time_section來表示
         $hours_key = date('H',strtotime($CheckTime));
         if ($hours_key >= 8 && $hours_key <= 11) $time_section = '早';
         elseif ($hours_key == 12) $time_section = '中';
         elseif ($hours_key <= 17) $time_section = '下';
         elseif ($hours_key <= 22) $time_section = '晚';
         else    die();
         //$_week存放著禮拜幾，$week_key取$CheckTime的weekday
         $_week=array('日','一','二','三','四','五','六');
         $week_key=date('w',strtotime($CheckTime));
         $sql_getAVGtime = "SELECT avgDiningTime FROM statistics WHERE shopid = '$shopid' AND section = '$_week[$week_key]/$time_section' AND tabletype = '$tabletype';";
         $row_getAVGtime = $mysql->query_row($sql_getAVGtime, $link);

       //算出user是取店內用餐第幾久的客人的用餐時間
         //自己的排隊號
         $queueUpNumber = $row_getQueue[0] + 1;

         //$queue_key 代表取第幾筆資料
         $queue_key = $queueUpNumber % $row_getTablenum[1];
         //等待人數還未超過店內一樣桌種桌數
         if($row_getQueue[0] <= $row_getTablenum[1]){
           return '1';
         }else{//等待人數超過店內一樣桌種桌數
           return '2';
         }
        }elseif($orderofnum>8){//訂位人數超過八人
            return "over eight people !";
        }else{
            return "Error !!!";
        }
    }
  }
?>
